﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LI4
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
            
        }

        

        //butao ok
        private void button1_Click(object sender, EventArgs e)
        {
            string username = usernameTB.Text.Trim();
            string password = passwordTB.Text.Trim();


            Data.Login login = new Data.Login();
            Program.Login = login.autenticar(username, password);


            int estado = Program.Login.Estado;


            if (estado == Business.Login.SUCCESS)
            {
                if (Program.Login.Tipo == Business.Login.OPERADOR || Program.Login.Tipo == Business.Login.SUPERIOR)
                {
                    string descricao = "Login no sistema: " + Program.Login.Username + ".";
                    Data.Registos regData = new LI4.Data.Registos();
                    Business.Registo registo = new Business.Registo(Program.Login.Username, descricao, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    regData.inserir(registo);

                    this.Hide();
                    new MenuPrincipal().Show();
                    
                }
                
                else
                    MessageBox.Show(this, "Nao tem premissao!", "Login.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (estado == Business.Login.BADLOGIN)
            {
                MessageBox.Show(this, "Login nao existe!", "Login.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                usernameTB.Text = "";
            }
            else if (estado == Business.Login.BADPASS)
            {
                MessageBox.Show(this, "Password Errada!", "Login.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                passwordTB.Text = "";
            }
        }

        private void Login_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void Login_Load(object sender, EventArgs e)
        {
            
        }

        private void Login_Shown(object sender, EventArgs e)
        {
            usernameTB.Focus();
        }

        
    }
}
